package com.base.data.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.base.data.dao.CityDao;
import com.base.data.entity.City;
import com.base.data.exception.DataException;
import com.mysteel.page.Page;
import com.mysteel.util.ObjectUtils;

@Service("cityService")
public class CityServiceImpl implements CityService
{
	@Autowired
	private CityDao cityDao;
	
	@Override
	public void saveCity(City city) throws DataException
	{
	
		Map<String,Object> map = new HashMap<String,Object>(2);
		
		map.put ("name" ,city.getName());
		map.put ("provenceId" ,city.getProvenceId ());
		City c= getCityByNameAndProvenceId (map);
		
		if(ObjectUtils.notNull (c)){
			throw new DataException(city.getName()+"已经存在，无需再添加!");
		}
		
		if (ObjectUtils.notNull (city))
		{
			for( int i = 1;i < 1000;i++)
			{
				String id = city.getProvenceId () + "0" + i;
				City ci = cityDao.get (id);
				if (ObjectUtils.isNull (ci))
				{
					city.setCreateTime (System.currentTimeMillis ());
					city.setId(id);
					
					cityDao.add (city);
					break;
				}
			}
			
		}
		
	}
	
	@Override
	public void deleteCityById(String id)
	{
		cityDao.delete (id);
		
	}
	
	@Override
	public City getCityById(String id)
	{
		
		return cityDao.get (id);
	}
	
	@Override
	public List<City> queryAll()
	{
		List<City> list = new ArrayList<City> ();
		
		for( String id : cityDao.query ())
		{
			list.add (cityDao.get (id));
		}
		
		return list;
	}
	
	@Override
	public List<City> pageCity(Map<String, Object> map)
	{
		
		return null;
	}
	
	@Override
	public void updateCity(City city)
	{
		City ci = cityDao.get (city.getId());
		if(ObjectUtils.notNull(ci)){
			ci.setName (city.getName());
			ci.setDescription (city.getDescription ());
			ci.setProvenceId (city.getProvenceId ());
		
			cityDao.update (ci);
		}
		
	}
	
	@Override
	public Page<City> page(Map<String, Object> params, int currentPageNum,int maxPageRowCount)
	{
		Page<City> page = null;
		
		if (ObjectUtils.isNull (params))
		{
			params = new HashMap<String, Object> (2);
		}
		
		int count = cityDao.count ();
		
		if (count > 0)
		{
			page = new Page<City> ();
			page.setTotalRowCount (count);
			page.setCurrentPageNum (currentPageNum);
			page.setMaxPageRowCount (maxPageRowCount);
			page.setURL (params);
			
			params.put ("startPageRowCount" ,page.getStartPageRowCount ());
			params.put ("maxPageRowCount" ,page.getMaxPageRowCount ());
			
			List<String> citys = cityDao.page (params);
			
			if (ObjectUtils.notEmpty (citys))
			{
				List<City> city = new ArrayList<City> (citys.size ());
				
				for( String cityId : citys)
				{
					city.add (cityDao.get (cityId));
				}
				
				page.setDatas (city);
			}
		}
		
		return page;
	}

	@Override
	public City getCityByNameAndProvenceId(Map<String, Object> map)
	{
		return cityDao.getByNameAndProvenceId (map);
	}
	
}
